package cn.jobstart.controller;

import cn.jobstart.common.SkList;
import cn.jobstart.common.StringUtil;
import cn.jobstart.dto.AdminJobCandidateDTO;
import cn.jobstart.dto.AdminJobCandidatePageDTO;
import cn.jobstart.enums.AdminJobCandidateIntentionFlagEnum;
import cn.jobstart.exception.ExceptionMessage;
import cn.jobstart.export.AdminJobCandidateDomainExport;
import cn.jobstart.modelExt.AdminJobCandidateExt;
import cn.jobstart.pubInter.ExcelExport;
import cn.jobstart.pubInter.IBaseService;
import cn.jobstart.pubInter.exception.BusinessException;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.AdminJobCandidateSearchExt;
import cn.jobstart.service.IAdminJobCandidateService;
import cn.jobstart.service.IAdminJobManagerService;
import cn.jobstart.vo.AdminJobCandidateDetailDomainVO;
import cn.jobstart.vo.AdminJobCandidateDetailVO;
import cn.jobstart.vo.AdminJobCandidateDomainVO;
import cn.jobstart.vo.AdminJobCandidateListVO;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author sunke
 * @Date 2023-09-06 15:00:20
 * @description AdminJobCandidateController      职位候选人
 */
@Api(tags = "人才管理系统-职位候选人相关", description = "职位候选人相关接口")
@RestController(value = "adminJobCandidateRestfulController")
@RequestMapping("/adminJobCandidate/")
@Validated
public class AdminJobCandidateController extends ApiManageController<AdminJobCandidateDTO, AdminJobCandidatePageDTO, AdminJobCandidateExt, AdminJobCandidateSearchExt, AdminJobCandidateDomainVO, AdminJobCandidateDetailDomainVO, AdminJobCandidateDetailVO, AdminJobCandidateListVO, String> {

    @Resource(name = "adminJobCandidateService")
    private IAdminJobCandidateService adminJobCandidateService;


    @Resource
    private IAdminJobManagerService adminJobManagerService;





    @Override
    public IBaseService<AdminJobCandidateExt, AdminJobCandidateSearchExt, String> getBaseService() throws SkException {
        return adminJobCandidateService;
    }


    @Override
    public void createExportExcelConditionExtend(AdminJobCandidatePageDTO pageDTO, HttpServletRequest request, HttpServletResponse response) {
        if (pageDTO.getSelfAddFlag() == 1 || pageDTO.getSelfAddFlag() == 0) {
            pageDTO.setSelfId(getJwtsUtil().getAdminId(request));
        }
        super.createExportExcelConditionExtend(pageDTO, request, response);
    }

    @Override
    public void searchConditionExtend(AdminJobCandidateSearchExt search, HttpServletRequest request, HttpSession session) throws SkException {
        String selfId=search.getSelfId();
        String adminId=selfId;
        if(StringUtil.isNullOrEmpty(selfId)){
            adminId=getJwtsUtil().getAdminId(request);
        }

        if (search.getSelfAddFlag() == 1) {
            search.setAdminIdEq(adminId);
        }else if(search.getSelfAddFlag() == 0){

            List<String> jobIds=adminJobManagerService.getJobIdsByAdminId(adminId);
            if(StringUtil.isNullOrEmpty(jobIds)){
               jobIds= SkList.getInstance().addObjToList("-1");
            }
            search.setAdminJobIds(jobIds);
        }


        super.searchConditionExtend(search, request, session);
    }

    @Override
    public boolean beforePostAdd(AdminJobCandidateDTO dto, AdminJobCandidateExt domain, HttpServletRequest request, HttpSession session) throws SkException {
        domain.setIntentionFlag(AdminJobCandidateIntentionFlagEnum.UN.getType());
        if (StringUtil.isNotObjEmpty(adminJobCandidateService.getDetailByResumeIdAndAdminJobId(domain.getResumeId(), domain.getAdminJobId()))) {
            throw new BusinessException(ExceptionMessage.ADMIN_JOB_CANDIDATE_EXIST);
        }
        domain.setAdminId(getJwtsUtil().getAdminId(request));
        domain.setAdminTruename(getJwtsUtil().getAdminTruename(request));
        return super.beforePostAdd(dto, domain, request, session);
    }



    @Override
    public String getBaseRoute() {
        return "/adminJobCandidate/";
    }

    /**
     * 当前模板名称
     *
     * @return
     */
    @Override
    public String getModelName() {
        return "职位候选人";
    }

    @Override
    public Class exportExcelClass() {
        return AdminJobCandidateDomainExport.class;
    }


    @Override
    public Class importExportExcelClass() {
        return AdminJobCandidateDomainExport.class;
    }


    @Override
    public Set<String> totalMapSet() {
        Set totalMapSet = new HashSet();
        totalMapSet.add("count(admin_job_candidate.ajc_id) as countNum");
        return totalMapSet;
    }


}